#import "RNCatalogDetailHeader.h"
#import "RNCategory.h"
#import "RNConstants.h"

@interface RNCatalogDetailHeader (RNCatalogDetailHeaderPrivateMethods)
-(void)bindControllerWithEntityName_:(NSString *)entityName withPredicate:(NSPredicate *)thePredicate inManagedObjectContext:(NSManagedObjectContext *)moc;
@end

@implementation RNCatalogDetailHeader

+(NSArray *)arrayOfHeadersWithRootObject:(id)rootObject filteredWithObject:(id)filterObject inManagedObjectContext:(NSManagedObjectContext *)moc {
    [RNCategory setShowsChildren:NO];
    RNCatalogDetailHeader *header1 = [[RNCatalogDetailHeader alloc] initWithRootObject:nil];
    [header1 setHeaderHeight:RNEmptyHeaderHeight];
    [header1 bindControllerWithEntityName_:RNEntityNameCategory withPredicate:[NSPredicate predicateWithFormat:@"parentCategory == nil"] inManagedObjectContext:moc];
    
    RNCatalogDetailHeader *header2 = [[RNCatalogDetailHeader alloc] initWithRootObject:nil];
    [header2 setHeaderHeight:RNHeaderHeight];
    [header2 bindControllerWithEntityName_:RNEntityNameItem withPredicate:[NSPredicate predicateWithFormat:@"category == nil"] inManagedObjectContext:moc];
    [header2 setTitleTemplate:@"with no category assigned"];
    [header2 setShowsTitle:YES];
    return [NSArray arrayWithObjects:header1, header2, nil];
}

-(void)bindControllerWithEntityName_:(NSString *)entityName withPredicate:(NSPredicate *)thePredicate inManagedObjectContext:(NSManagedObjectContext *)moc {
    NSArrayController *controller = [self primaryController];
    [controller setEntityName:entityName];
    [controller setAutomaticallyRearrangesObjects:YES];
    [controller setManagedObjectContext:moc];
    [controller setFilterPredicate:thePredicate];
    [controller prepareContent];
    [self setPrimaryController:controller];
}

@end
